<?php

namespace app\api\controller;

use app\BaseController;
use app\api\validate\User;
use app\common\business\User as userBus;
use think\Exception;

class Login extends BaseController
{
    public function index()
    {
        $isPost = $this->request->isPost();
        if (!$isPost) {
            return show(config("status.error"), "请求方式不正确");
        }

        $phone_number = input("param.phone_number", '', 'trim');
        $code = input("param.code", '', 'trim');
        $type = input("param.type", '', 'trim');
        $data = [
            'phone_number' => $phone_number,
            'code' => $code,
            'type' => $type,
        ];
        $validate = new User();
        if (!$validate->scene('login')->check($data)) {
            return show(config("status.error"), $validate->getError());
        }
        $userObj = new userBus();
        try {
            $result = $userObj->login($data);
        } catch (Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }
        if ($result) {
            return show(config("status.success"), "登陆成功", $result);
        } else {
            return show(config("status.error"), "登陆失败");
        }


    }
}